#!/usr/bin/python
# -*-coding:utf-8 -*-
import httplib2
import urllib2
import re #正则表达式模块

class WeiboClass: #定义一个weibo类

    #获取指定url的网页内容
    def get_content(self,url,headers,id):
        http=httplib2.Http()
        response,content=http.request(url+str(id),'GET',headers=headers)
        #print url+str(id)
        return content.decode('unicode-escape').encode('utf-8')

    #判断weibo的用户是否是企业用户
    def is_company(self,url,headers,id):
        content=self.get_content(url,headers,id)
        title=r'行业'
        company_title=re.compile(title) 
        if company_title.search(content): #使用正则表达式对title进行匹配        
            return 1
        else:
            return 0
        
    #获取用户的weibo信息：ID，主页url，昵称
    def get_info(self, url,headers,id):
        flag=self.is_company(url,headers,id)
        content=self.get_content(url,headers,id)
        if flag==0:  #如果用户是个人用户            
            #print content
            #微博ID
            id_flag=r'\$CONFIG\[\'oid\'\] = \'([0-9].+?)\';'
            id_re=re.compile(id_flag)
            id_regx=id_re.search(content)
            id=id_regx.group(1)
            print id

            #微博url
            url_flag=r'<meta http-equiv="mobile-agent" content="format=xhtml;" url="weibo.cn/(.+?)\?'
            url_re=re.compile(url_flag)
            url_regx=url_re.search(content)
            url_0=url_regx.group(1)
            url='http://weibo.com/'+url_0
            print url
            
            #昵称
            name_flag='<div class="name clearfix">.+?<div class="left">(.+?)<'
            name_re=re.compile(name_flag,re.S)
            name_regx=name_re.search(content)
            name=name_regx.group(1)
            name=name.decode('utf-8').encode('GBK')
            print name

def main():            
    headers={"cookie":'NSC_wjq_xfjcp.dpn_w3.6_w4=ffffffff0941137b45525d5f4f58455e445a4a423660; SUS=SID-1806453925-1342851885-XD-hgrjc-03210d75ca203f3ad0d57666a05ae49d; SUE=es%3Deabd0a14bc6e6123c5c4d058d9a2c96f%26ev%3Dv1%26es2%3Db50ae59b82b457a1ba54b2b7708fbb5b%26rs0%3DdP4RVYzORRwV64PFw6wRdNGBk0HP47V8C5SXUp%252F7Q9K2RcduYt4ECQbEDNZk%252Bs8GHDpW5wk%252B3%252FmYKP12zIyQbD1bUMd2wNBgdRX45p2rygizXgHMjH%252FFnU53HJFC2OfSvEHZADJkZD%252BTdLHidgoyy4maajVHi%252B%252B1en0zIKIf3mo%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1342851885%26et%3D1342938285%26d%3Dc909%26i%3D4dc3%26us%3D1%26vf%3D0%26vt%3D0%26ac%3D0%26uid%3D1806453925%26user%3Dqipeng.sdsy%2540163.com%26ag%3D4%26name%3Dqipeng.sdsy%2540163.com%26nick%3D%25E4%25B8%2580%25E5%258C%25B9%25E5%259C%25A8%25E8%25B7%25AF%25E4%25B8%258A%25E7%259A%2584%25E9%25A9%25AC%26fmp%3D%26lcp%3D; SSOLoginState=1342851885; ads_ck=1; UOR=hao.360.cn,weibo.com,spr_web_360_hao360_weibo_t001:1342851234578; _olympicMedalsTable=; ULV=1342851891453:53:53:53:3919105774821.296.1342851890484:1342851070843; SinaRot/u/1806453925=25; un=qipeng.sdsy@163.com; __utma=182865017.1486634646.1342787238.1342787238.1342787238.1; __utmz=182865017.1342787238.1.1.utmcsr=weibo.com|utmccn=(referral)|utmcmd=referral|utmcct=/u/1806453925; myuid=1806453925; wvr=4; ALF=1343135290; SinaRot/u/1806453925%3Fgid%3D201108260278357557=77; SinaRot/u/1806453925%3Fc%3Dspr_web_360_hao360_weibo_t001=59; _s_tentry=-; Apache=3919105774821.296.1342851890484; SINAGLOBAL=3919105774821.296.1342851890484; BAYEUX_BROWSER=4e70u68yfp1x9xreh4wbcw2jfhs; JSESSI\
ONID=3nvoas5ykh0e'}    
    url='http://weibo.com/'
    print headers
    page = WeiboClass()
    page.get_info(url,headers,1842764911)


if __name__ == "__main__":
    main()